<?php

namespace app\wechat\controller;

use app\common\model\ConfigModel;

/**
 * Class Config
 * @package app\wechat\controller
 */
class Config extends \think\Controller
{

    public function index()
    {
        $arr = $this->ioAll();
        $configModel = new ConfigModel();
        $readConfig = $configModel->where(["config" => "default"])->find();
        return ($readConfig[$arr['item']] == 1) ? $this->ok("hello world!") : $this->fail("hello world!");
    }

    /**
     * 保存配置
     *
     * @return \think\response\Json
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function save()
    {
        $arr = $this->ioAll();
        $configModel = new ConfigModel();
        $configSave = $configModel->save($arr, ["config" => "default"]);
        if (isset($arr['barrager'])) {
            $configModel->query("TRUNCATE TABLE `we_barrager`;");
        }
        if (isset($arr['shake'])) {
            $configModel->query("UPDATE `we_user_info` SET `gameTime` = NULL , `times` = '0';");
        }
        return $configSave ? $this->ok($configSave) : $this->fail("保存配置失败");
    }
}
